<?php
App::uses('AppModel', 'Model');
App::import('Model', 'HistoricoEmprestimo');
/**
 * Emprestimo Model
 *
 * @property Alunos $Alunos
 * @property Livros $Livros
 */
class Emprestimo extends AppModel {
/**
 * Primary key field
 *
 * @var string
 */
	public $primaryKey = 'id_emprestimos';
/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'id_emprestimos' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'alunos_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			'existente'=>array(
				'rule'=>'existeAluno',
				'message'=>'Aluno não existe no cadastro'
			)
		),
		'livros_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'retirada' => array(
			'date' => array(
				'rule' => array('date'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'devolver_em' => array(
			'date' => array(
				'rule' => array('date'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
	public $belongsTo = array(
		'Aluno' => array(
			'className' => 'Aluno',
			'foreignKey' => 'alunos_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Livro' => array(
			'className' => 'Livro',
			'foreignKey' => 'livros_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);
	
	
	public function calculaDevolucao($dias){
		$timestamp = strtotime("+$dias days");
		if(date('N', $timestamp) == 7) // DOMINGO
			$dias++;
		if(date('N', $timestamp) == 6) // SABADO
			$dias += 2;
		$data = date('Y-m-d', strtotime("+$dias days"));
		$data = explode('-', $data);
		return array('day'=>$data[2], 'month'=>$data[1], 'year'=>$data[0]);
	}
	
	function procuraPorLivroAluno($codigo, $id, $exemplar){
		return $this->find('first', array('conditions'=>array('livros_id'=>$codigo,'alunos_id'=>$id, 'Emprestimo.exemplar'=>$exemplar)));
	}
	
	public function finalizarEmprestimo($id){
		$histEmprestimo = ClassRegistry::init('HistoricoEmprestimo');
		$emprestimo = $this->findByIdEmprestimos($id);
		$dadosEmprestimo = $emprestimo;
		$emprestimo = $emprestimo['Emprestimo'];
		unset($emprestimo['id_emprestimos']);
		unset($emprestimo['devolver_em']);
		$emprestimo['devolvido'] = date('Y-m-d');
		$this->id = $id;
		$dataSource = $this->getDataSource();
		$dataSource->begin();
		if ($histEmprestimo->save($emprestimo)){
			if ($this->delete()){
				$dataSource->commit();
				return $dadosEmprestimo;
			}
		} else
		 $dataSource->rollback();
		 return false;
	}
	
	public function existeAluno($data){
		if($this->Aluno->findByIdAlunos($data['alunos_id']))
			return true;
	}
}
